#!/usr/bin/python
import binascii
import struct
import pylab
import numpy
import scipy
import os

def sc_to_fft(sc_index):
    """
        input: sub carrier index
        output: fft index
        ieee802.16e spec, dc subcarrier is index 512, counted from 0
        fft size 1024 only
    """
    if sc_index <512:
        return (sc_index+512)%1024
    else:
        return sc_index-511

def get_preamble(fft_size, index, frequency_domain=False):
    '''
    modulate sequence, 1024 FFT, IDcell=0, segment = 0
    '''

    wk    = [
                 '0xA6F294537B285E1844677D133E4D53CCB1F182DE00489E53E6B6E77065C7EE7D0ADBEAF',
                 '0x668321CBBE7F462E6C2A07E8BBDA2C7F7946D5F69E35AC8ACF7D64AB4A33C467001F3B2',
                 '0x1C75D30B2DF72CEC9117A0BD8EAF8E0502461FC07456AC906ADE03E9B5AB5E1D3F98C6E',
                 '0x5F9A2E5CA7CC69A5227104FB1CC2262809F3B10D0542B9BDFDA4A73A7046096DF0E8D3D',
                 '0x82F8A0AB918138D84BB86224F6C342D81BC8BFE791CA9EB54096159D672E91C6E13032F',
                 '0xEE27E59B84CCF15BB1565EF90D478CD2C49EE8A70DE368EED7C9420B0C6FFAF9AF035FC',
                 '0xC1DF5AE28D1CA6A8917BCDAF4E73BD93F931C44F93C3F12F0132FB643EFD5885C8B2BCB',
                 '0xFCA36CCCF7F3E0602696DF745A68DB948C57DFA9575BEA1F05725C42155898F0A63A248',
                 '0x024B0718DE6474473A08C8B151AED124798F15D1FFCCD0DE574C5D2C52A42EEF858DBA5',
                 '0xD4EBFCC3F5A0332BEA5B309ACB04685B8D1BB4CB49F9251461B4ABA255897148F0FF238',
                 '0xEEA213F429EB926D1BDEC03ABB67D1DE47B4738F3E929854F83D18B216095E6F546DADE',
                 '0xC03036FA9F253045DF6C0889A8B83BAEFCF90EB993C2D79BD911CA84075061AA43DA471',
                 '0x1E68EC22E5E2947FB0A29E4CC70597254B36C60331EACF779FE752D3F55DC41ABFC7DC9',
                 '0x63A57E75A0434F035AAC4504B265081D497F10C77928B71797C5D6C6824DC0F23BE34EE',
                 '0xC57C4612816DE981C58FD6F8DE9DD41F2422ADBC522B0CE31F9A6D5F2A126DC08F69FB1',
                 '0x978256AF184E7ED17789B33D324C711B36BFBCCE5446EB03687E9A0A839C7CE156104D2',
                 '0x011EC823157DD73150640CEB7DDB0A1F8F91E09599A851D5C7CAF687CFB752D297D82FC',
                 '0xC6DE82BEB7F57B9120E8A376D85C8F70FDC65BC660402DAC4AE6002EA2740C4F9E5973C',
                 '0x4C74929D6F9FAB9E5BB761026038E076F6824295E0AF397806ECEBC6DC713F03ACDC27C',
                 '0x13E1E85C2234D0F3418001A35F135E10C6C918C36BC659FDA9D655D288A0BDAA8BF489D',
                 '0xFD4AF2D8F4F08F1A7DF59291C9AEE788F641B8231CFB813376E0BEB68DFCFCBBE552445',
                 '0xEBBC77A493AA0C62C62F25EE5E8D0701F50386F49026FA31487C9FD5C5206CE4EB00576',
                 '0x134F936F9E875842587ADCA92187F2FC6D62FFC3A833D8CDE465F9972ABAA83763AAEB7',
                 '0x3CD1DA70670BC73363D1B4A66D280FF6AA7636D07ECF32BA26101E5EBA1594FB8A0420A',
                 '0x918296B2937C2B6F73CF98F85A81B723D1C69DBDF3E019749C582DA22E789562729D475',
                 '0xC323981B8B2240865F48D61AE1B3B61D88522B7358952F949D4308CA15D1EE8FDFA683F',
                 '0x7514A6FA5FBB250C5C8CE96F791D676036C344A44B24284477B44CB3E758F8BCD58F05B',
                 '0x84C7FEC6E977FA1EC0C7CC9E0D067C73D8F846F82ABB3456D2104E1448D5A58D5975152',
                 '0x4841AFC277B86A0E067AF319422F501C87ACBFBDD66BFEA3644F879AE98BA8C5D605123',
                 '0xF35EA87318E459138A2CE69169AD5FD9F30B62DA04ED21320A9F59893F0D176752152FD',
                 '0xA0C5F35C5971CD3DC55D7D2B9FD27AA17A198583F580EB0800744EE5B6B3648DEA95840',
                 '0xA6D3D33AD9B56862DBF076E3ACE6A3150510CCC8BE77DE4E6E10EB5FE163765647D07DF',
                 '0x52849D8F020EA6583032917F36E8B62DFD18AD4D77A7D2D8EC2D4F20CC0C75B7D4DF708',
                 '0xCC53A152209DEC7E61A06195E3FA633076F7AE1BAFFE83CE565087C0507BA596E0BD990',
                 '0x17D98A7E32CCA9B142FE32DB37B2BF726E25AA7A557FFB5C400B47A38B16CF18E1EDE63',
                 '0xA5BA8C7E2C795C9F84EBBD425992766BDE5549A7A9F7EF7E44AFD941C6084568638FE84',
                 '0x33E57E78A5696255CA61AE36027036DA619E493A0A8F95D9915C6E61F3006CB9706BEBA',
                 '0x09961E7309A9B7F3929C370C51910EBAB1B4F409FA976AE8679F354C84C4051F371F902',
                 '0x508A9EBAEF3C7E09CFCFC0B6F444A09B45A130EFC8C5B22BCE87213854E7C9D329C9ADC',
                 '0xAACEEF9BCDC82E4AD525185B07CBABCB74861D16F7C25CFBA917B05463AD65391AF840D',
                 '0x23060ACC5A125DAB207EEEE47B4EEE1E8466BD17DDA2EB3CD90D2AB7A758C213E6D7FE5',
                 '0xCA55521667BDA8B6F1B205201A51B3A0C05DE9EA06BC73268730A81A992777021F46055',
                 '0x05ADFCA2F8207DC6FF8D1A85A1DD4694D4C48A838C4F833C532710021AC448A7B62B8DD',
                 '0x218C951223D7B712DC98F8B5217388A830003C5F2A00F232DD3475D2FC78C25B8D88FF9',
                 '0x79B94D24D721121EF678B7156F8D2666DE712BBF3837C85A9518781903146A7B4D42A28',
                 '0x58AABEF6A6BDE4011CAC583C5104B2C6FC5A2980F856373E5931A3C690245327581FA13',
                 '0x427D1AD18E338E16FCE6E23B4AD6D82A2144D53048F2665AA94577AFABD26889FCB1F9F',
                 '0x337FE0E4C15A22471AE0F6B6F91161A7DE2E1403D73587D5C8355105D2F70642B2CE425',
                 '0xA3FCAA311B536AC9DB39FED9F4E996506B3181C58D6B7E04157A3FD463F60468765BCFD',
                 '0xF484FD1F57F53A4A749B86148E0B1D0653667CE1393198875DDB0AE9179BBBDAAD53A11',
                 '0xA3E9ECF1E6048562BC89DB6168E708855F0D4AD29F859EF36C9160DF407D85426233632',
                 '0x890519376D1FFAA2894EABCD6663B0A3C2411982C17B01270E0FB0B289D4BC8C3B83DA9',
                 '0x09847B6187BB5F6F6728B4ED610088FAD9DADFC00748E9DCD8A0CE320D6C991654ABE05',
                 '0x3285AE0A3D196313659C37BE1C94D61D20F11FD49D9FDF9D1026FF5763F02CB78AE135C',
                 '0x0069D3F34D0D455AFB45FEFDF716333B785C6BDA90DA23F1CC68BC6A1DBC916C595DA3E',
                 '0xAA977A8BCA39381E7C35A1ACC7C4F60421C0862BFD6106C7C025B0676EA0EF68972DD8F',
                 '0xF310745C497094ABE56E0490C0800319DBE290553E696B6859635AF03B121F79D925D19',
                 '0x964DFD350B9C7DFDC7F6F7C43283A76F0D613E48A5520D1DAF761C6F47E389B43A023F5',
                 '0x6D767B88D28A455CC3B56C942BAFD8E465A50FD2C22FE6162E03A9AAC3C1CC899800610',
                 '0xC5491C6CA3D998906EC1482F815B74B7C2E3816B682ACC6009AB7EFF34BF0E9CE59C754',
                 '0x6D8EE32D30E19D93A0E5AD8226BAE9CF6FCBA17CF6E67FDC5A15A81ECB8908BEDD77C80',
                 '0x98F8BFDF774C7A249418E6FF4723D6E6AB2F091CDE4DE1CE11D3BD463B509FB716940FD',
                 '0x65300BAD8FFA21BC7DC2C1F79FA97A9F469CCC9E270A61759F34D6276F57CBEB009CD21',
                 '0x6F36BB6D5A7DC4FB720439E91FF0DE86DD6C4B93CFC4271F2BCC6169616E3AEAA19E360',
                 '0xD27B00C70A8AA2C036ADD4E99D047A376B363FEDC287B8FD1A7794818C5873ECD0D3D56',
                 '0xE7FDDCEED8D31B2C0752D976DE92BEA241A713CF818C274AA1C2E3862C7EB7023AF35D4',
                 '0x87BF4954022D30549DF7348477EACB97AC3565B838460CC62F242883313B15C31370335',
                 '0x82DD830BEDE4F13C76E4CF9AEF5E42609F0BDDCB000A742B6372DD5225B0C3114494746',
                 '0x4E06E4CF46E1F5691938D7F40179D8F79A85216775384BD97966DB4BBF49FB6FAB8F945',
                 '0x64164534569A5E670FDB390D09C04802DD6A16B022CADC77EDD7464AFED43C773A8DC76',
                 '0xFB8769A81AA9DB607F14A6A95948401F83057CDC9C9C3996BA5821403A49F00A4E35191',
                 '0x77710D6F40B4F79CC63F678551C3EC18FA9DF2C82E6C8F415DADFD63264B7513180070E',
                 '0x503F196BBF93C238BFD5E735E5AE52E0DAE64F5E2F4C3B92E553F51303C4A64C4403BF3',
                 '0x5FD4A6894566678C95B9D5A59DDE5366799045FEB03A2BAA74094140E9068C61C2E972C',
                 '0x95B584DC40C8B5DEAD63D48FCE65B1E61BAB4C597D921DB12677141E2FFE7C0AA3DA0D5',
                 '0x985763AB6CC8934DB8A0BE738A7AF1D1FA3958C1F9E2D6A51A163E47A0A6E5FEB759FDD',
                 '0xFD8D45F00D943AD986BD353D61C6746DBF8A309B6AE1C173B880D957B76DC031A957E8D',
                 '0xAE4323534F6EFB1A20169328417885EF304FA220389FA9C2607E5A406F4CE4A7498A39F',
                 '0xE5205579893BE184CB9948C28E2F9AAF699D47B6E5E0B219CBEAFE4BEC8D561BD809E34',
                 '0xAB11D6941478D36D5695CE813070DC1E32122A39083E53FE373660AEB125D83383FBDCA',
                 '0x188A09C46F1F11206FF9F15CFB5F6CD2F26C4BF485EE37D3650A595064F76CE34E40EAD',
                 '0x4B1CDE25539A56CEDC45FE7F54C38CF155F4FB1AE868F6C3952D07014BF828E810BDE2D',
                 '0x16CA8F8C6A879E865E3611EAC389D56AFA3E4E84CDBB73567BA4A160249C4B680A7D9BC',
                 '0x39D2B08AA0E2E8781476027B41AD72F8D9838B7001AADFD33A92D81E56ECBB2C9378D58',
                 '0x8C258BC80D4AD125F335A5151EDF9E9A463E06C5C8D046F82E5DC3D73EF4D2231C5D14F',
                 '0x41A029C6356C825585179C5348EDF07A3AC2022539AC28DC4CD3C1DFADC8EE9644CD939',
                 '0x0D70A77CBE9804913BFBEC4FBF917C5CD3580F6062BBAD3F99ECEBB4A9EBB87523AB722',
                 '0x6A00A30901F9FDE44B4F1ECED44E0BCB943B29519F313BE4496D34F39B154FC2384CB75',
                 '0x95351107A8BE6ABFC24C1292FE1A0FE677CBFD04F2E81178CAA9D294730EF9C946F676E',
                 '0x01F21470FD9B1E0B3C6B2F7C0412A15764C277D61BA2EE3B3769DE7ADACB2BB29918FB7',
                 '0xA578ABFE155369440FA3D4DF757CCA596469B80A0E56BFE6010DD63E67CEDB86BB1EF39',
                 '0x1E1CFFAB031836777DE5D168A9246C559574C74CCC06405EB406B8DDB7C9A6EF54A66A5',
                 '0x354149C2CA19A735F9CD04AF4922E8ECE6509B978B951F946FD4AD36C7F9C83624205E7',
                 '0x5A27E60DEA547D0D41897A03199F28A967AC51728E3B38325B4FBECF1B85A7EE9B04182',
                 '0x784DA3B16B810FE3B851060AD7BD27D9D9457F6C8899A13D311E531B855C15ECE6D3A2F',
                 '0xD7DFBC65797633A8C13D3EEC781D48952338136063B579D69437B28B744B5A4BE18AFA9',
                 '0x61AF26BD39A9FFF52826625E04ADA299385A373FA946D837D754E6CFEBB26F5C03B87CF',
                 '0xD77D97CDB93DBEAA65CAFA146F40D72B5E80944F750E07325DC164ED60F32434BC7187D',
                 '0x4529D9CA65AF49C1C39BDC18CFAB87E03FE4DAFC0A48FF1457D46B0DF66B414A23ACDDB',
                 '0x33AC0261DAA57C1D611EBA1C730D50AFEE5BE3E849030A4E891BC8C5F4C78DCDDFEA263',
                 '0xBED48C704F02A84F03BCD299D919DA56F7B71EDF8A0F8A25E8F8496F95A44CE2B9F74C9',
                 '0x0ECCBE0902EBF4B4C29506014A3706622784B7B2D5153E10AD3112DC5E45277A32E79DE',
                 '0x7CB4937889C7DFD9AA2D37235E06F993D3D4F5D515B39CA652F62397C08457D66BC5A36',
                 '0x43F23F6CAC6C43896B3EDBF00E1CBD42E2CC75E2A996448F0FCF17F6779DD6E356FED11',
                 '0x72C8A209FBC4A568BEF03BCFE1B0D959F977B0963780B4E54E2B9A1016344ACB7EE3E3A',
                 '0x77AEB9E50DC3727849A94FBFFCDB5B9589AF50ABD8A58808B9663058E17A2EBC496DF43',
                 '0x667123C89077FE4AAAEF15C635E976C6811682D478FFC7B721A76B5A38697DF4FB7D2CE',
                 '0xCBD6C5C9BE55B0BE76AD03392E8A8AB9A86063DB31B79280B447980BB841FD7E9DC6B9B',
                 '0xC7D7DEF8B3C9C8667D8D65063B4DAD1FF69445C87CA71DA955D0CA23970E988A6EA4C83',
                 '0xFB246ABD92F9E560CB2BEC2317204C9CE22AD3BD19EA02E90F5F3B7F4F65538D8ED098E',
                 '0x29E74579472FDD8FFC2700B2BF33C649989DD8153093A7CA08B50F7A5E4BAED108A0F0D',
                 '0xA27F29D8D6CCD7EB4BBE303C3E9E95802DB98BFD5B8ED03B88304359D92E3EC108CA3C8',
                 '0x3FE70E26FA00327FE3B2BE6BC5D5014F588F09C17D222C146DD68B4824692A651888C76',
                 '0x41E91307EC58801CFF2C7E9CFEFBEB71681FAE2BEAEC72D4E4556E99345D3BA4B369B59'

            ]

    if fft_size!=1024:
        err_msg = 'fft size other than 1024 not supported'
        raise Exception(err_msg)
        

    bits  = [0]*288
    bytes = binascii.a2b_hex(wk[index][2:]+'0') # cut off leading 0x and append 0 to byte boundary
    bit_index = 0
    for byte in bytes:
        byte = struct.unpack('B',byte)[0]
        for k in range(1,9):
            bit = (byte & 0x80)
            if bit:
                bits[bit_index] = -1
            else:
                bits[bit_index] = 1
            bit_index += 1
            byte = (byte*2)&0xff


    preamble_freq = [0]*1024

    if index < 96:
        div, mod = divmod(index, 32)
        segment = div
    else:
        segment = index%3
        
    sc_index = segment
    for index_bit in xrange(284):
        if ((sc_index%3)==segment) and sc_index!=512:
            preamble_freq[sc_to_fft(sc_index+86)] = float(bits[index_bit])
        sc_index += 3
            
    if frequency_domain:
        return preamble_freq

    preamble_time = pylab.ifft(preamble_freq)
    data_real = scipy.real(preamble_time)
    data_imag = scipy.imag(preamble_time)
    return preamble_time
